package com.gwtsz.chart.output.customer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.TextUtils;
import com.gwtsz.chart.components.YAxis;
import com.gwtsz.chart.renderer.YAxisRenderer;
import com.gwtsz.chart.utils.Transformer;
import com.gwtsz.chart.utils.Utils;
import com.gwtsz.chart.utils.ViewPortHandler;

/**
 * Created by spence on 17-3-15.
 */

public class GWYAxisRenderer extends YAxisRenderer {

  protected YAxis mYAxis;
  public GWYAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
    super(viewPortHandler, yAxis, trans);
    mYAxis=yAxis;
  }


  @Override
  protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
    float height=(mViewPortHandler.contentBottom()-mViewPortHandler.contentTop());
    for (int i = 0; i < mYAxis.mEntryCount; i++) {

      String text = mYAxis.getFormattedLabel(i);
      if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
        return;

      int labelHeight = Utils.calcTextHeight(mAxisLabelPaint, text);
      float pos = height-i*height/(mYAxis.mEntryCount-1) + offset;

      if ((pos - labelHeight) < mViewPortHandler.contentTop()) {

        pos = mViewPortHandler.contentTop() + offset * 2.5f + 3;
      } else if ((pos + labelHeight / 2) > mViewPortHandler.contentBottom()) {
        pos = mViewPortHandler.contentBottom() - 3;
      }else if(pos>height/2){

      }

      c.drawText(text, fixedPosition, pos, mAxisLabelPaint);
    }

    }


  @Override public void renderGridLines(Canvas c) {
    if (!mYAxis.isEnabled())
      return;

    // pre alloc
    float[] position = new float[2];

    if (mYAxis.isDrawGridLinesEnabled()) {

      mGridPaint.setColor(mYAxis.getGridColor());
      mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());
      mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());

      Path gridLinePath = new Path();
      float height=(mViewPortHandler.contentBottom()-mViewPortHandler.contentTop());
      float yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();
      // draw the horizontal grid
      for (int i = 0; i < mYAxis.mEntryCount; i++) {

        float pos = height-i*height/(mYAxis.mEntryCount-1) + yoffset;
        gridLinePath.moveTo(mViewPortHandler.offsetLeft(), pos);
        gridLinePath.lineTo(mViewPortHandler.contentRight(), pos);

        // draw a path because lines don't support dashing on lower android versions
        c.drawPath(gridLinePath, mGridPaint);

        gridLinePath.reset();
      }

    }

    if (mYAxis.isDrawZeroLineEnabled()) {

      // draw zero line
      position[1] = 0f;
      mTrans.pointValuesToPixel(position);

      drawZeroLine(c, mViewPortHandler.offsetLeft(), mViewPortHandler.contentRight(), position[1] - 1, position[1] - 1);
    }
  }
}
